package com.site.wisdom_construction.controller.trainingCheckModule;

import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.trainingCheckModule.ExamQuestionsService;
import com.site.wisdom_construction.service.trainingCheckModule.ExaminationPaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.google.common.primitives.Ints.concat;

/**
 * @Author daiyifei
 * @Date 2022/1/17
 * @Description
 */
@Api(tags = "考试试卷接口")
@RestController
@RequestMapping("ExaminationPaper")
@CrossOrigin
public class ExaminationPaperController {
    @Autowired
    private ExaminationPaperService examinationPaperService;

    @Autowired
    private ExamQuestionsService examQuestionsService;

    @ApiOperation(value = "增加考试试卷", notes = "测试数据:")
    @PostMapping("/insertExaminationPaper")
    public JsonResult insertExaminationPaper(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = examinationPaperService.insertExaminationPaper(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功录入考试试卷!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("录入考试试卷失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "快速组卷", notes = "测试数据:")
    @PostMapping("/insertExaminationPaperByFast")
    public JsonResult insertExaminationPaperByFast(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        //获取选择题数组
        List<Integer> choiceList = examQuestionsService.findExamQuestionsByChoice(map);
        //获取多选题数组
        List<Integer> multipleChoiceList = examQuestionsService.findExamQuestionsByMultipleChoice(map);
        //获取判断题数组
        List<Integer> trueList = examQuestionsService.findExamQuestionsByTrue(map);
        //获取填空题数组
        List<Integer> gapList = examQuestionsService.findExamQuestionsByGap(map);

        List<Integer> allList = new ArrayList<Integer>();
        allList.addAll(choiceList);
        allList.addAll(multipleChoiceList);
        allList.addAll(trueList);
        allList.addAll(gapList);


        String paper = allList.toString();
        //去除中括号
        String regEx="[\n`~!@#$%^&*()+=|{}':;'\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。， 、？]";
        String a ="";
        String paperContent = paper.replaceAll(regEx,a);
        map.put("paperContent",paperContent);

        int i = examinationPaperService.insertExaminationPaper(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功录入考试试卷!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("录入考试试卷失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除考试试卷信息", notes = "测试数据:")
    @PostMapping("/deleteExaminationPaper")
    public JsonResult deleteExaminationPaper(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        List<Integer> list = (List<Integer>) map.get("list");
        int l = 0;
        Map mapR = new HashMap();
        for (int i = 0;i<list.size();i++){
            mapR.put("id",list.get(i));
            int a = examinationPaperService.deleteExaminationPaper(mapR);
            l+=a;
        }
        if (l>=1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功删除考试试卷!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除考试试卷失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改考试试卷", notes = "测试数据:")
    @PostMapping("/updateExaminationPaper")
    public JsonResult updateExaminationPaper(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = examinationPaperService.updateExaminationPaper(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功修改考试试卷!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改考试试卷失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页模糊查询考试试卷", notes = "测试数据:")
    @PostMapping("/findExaminationPaperByPage")
    public Page findExaminationPaperByPage(@RequestBody Map map) {
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(examinationPaperService.Total(map));
        page.setItems(examinationPaperService.findExaminationPaperByPage(map));
        return page;
    }

    @ApiOperation(value = "根据id查询考试试卷", notes = "测试数据:")
    @PostMapping("/findExaminationPaperById")
    public OneDataResult findExaminationPaperById(@RequestBody Map map) {
        OneDataResult oneDataResult = new OneDataResult();
        oneDataResult.setData(examinationPaperService.findExaminationPaperById(map));
        return oneDataResult;
    }

    @ApiOperation(value = "下拉框查询所有试卷", notes = "测试数据:")
    @PostMapping("/selectExaminationPaper")
    public OneDataResult selectExaminationPaper(@RequestBody Map map) {
        OneDataResult oneDataResult = new OneDataResult();
        oneDataResult.setData(examinationPaperService.selectExaminationPaper(map));
        return oneDataResult;
    }
}
